package com.ebupt.migu.music.autoTri.Controller;

import com.ebupt.migu.music.autoTri.Service.AutoTriService;
import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2020/6/9
 */
@RestController
@RequestMapping("/autoTri")
public class AutoTriController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private AutoTriService autoTriService;
    @Autowired
    private IDAuthUtils idAuthUtils;

    /**
     * 创建运营计划
     *
     * @return
     */
    @RequestMapping("/createOperPlan")
    public ResultObject createOperPlan() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoTriService.createOperPlan(params);
    }

    /**
     * 更新运营计划
     *
     * @return
     */
    @RequestMapping("/updateOperPlan")
    public ResultObject updateOperPlan() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoTriService.updateOperPlan(params);
    }

    /**
     * 查询运营计划列表
     *
     * @return
     */
    @RequestMapping("/queryOperPlan")
    public ResultObject queryOperPlan() {
        return autoTriService.queryOperPlan();
    }

    /**
     * 创建自动触发策略
     *
     * @return
     */
    @RequestMapping("/createAutoTri")
    public ResultObject createAutoTri() {
        // 获取用户信息和业务参数
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        return autoTriService.createAutoTri(params);
    }

    /**
     * 查询策略列表
     *
     * @return
     */
    @RequestMapping("/queryAutoTris")
    public ResultObject queryAutoTris() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return autoTriService.queryAutoTris(params);
    }

    /**
     * 查看指定策略详情
     *
     * @return
     */
    @RequestMapping("/queryAutoTriDetail")
    public ResultObject queryAutoTriDetail() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoTriService.queryAutoTriDetail(params);
    }

    /**
     * 修改策略详情
     *
     * @return
     */
    @RequestMapping("/updateAutoTri")
    public ResultObject updateAutoTri() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdminOrAuditor(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权操作此策略");
        }
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return autoTriService.updateAutoTri(params);
    }

    /**
     * 暂停策略
     *
     * @return
     */
    @RequestMapping("/pause")
    public ResultObject pause() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdmin(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权操作此策略");
        }
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return autoTriService.pause(params);
    }

    /**
     * 策略审批
     *
     * @return
     */
    @RequestMapping("/doApprove")
    public ResultObject doApprove() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return autoTriService.doApprove(params);
    }

    /**
     * 查询策略审批列表
     *
     * @return
     */
    @RequestMapping("/queryApproveRecords")
    public ResultObject queryApproveRecords() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return autoTriService.queryApproveRecords(params);
    }

    /**
     * 查询指定策略审批记录
     *
     * @return
     */
    @RequestMapping("/queryApproveRecordByTacID")
    public ResultObject queryApproveRecordByTacID() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoTriService.queryApproveRecordByTacID(params);
    }

    /**
     * 撤回策略
     * <br>
     * 在创建策略后审批前，提供撤回策略选项，撤回后将策略状态改为待提审
     *
     * @return
     */
    @PostMapping("/revocation")
    public ResultObject<String> revocation() {
        // 验证登录账号是否能执行该操作
        if (!idAuthUtils.isCreatorOrAdminOrAuditor(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权撤回此策略");
        }
        return autoTriService.revocation(ReqestParamUtil.getParamsWithUserInfo(request));
    }

    /**
     * 实时测试
     *
     * @return
     */
    @RequestMapping("/manual")
    public ResultObject manual() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoTriService.manual(params);
    }
}
